﻿using Bll;
using DecorationSys.Models;
using Entity;
using Entity.DTO;
using Entity.ViewModel.OrderMaterial;
using IBll;
using Microsoft.AspNetCore.Mvc;

namespace DecorationSys.Controllers
{
    /// <summary>
    /// 订单材料相关
    /// </summary>
    public class OrderMaterialController : Controller
    {
        IOrderMaterialBll _orderMaterialBll;

        public OrderMaterialController(IOrderMaterialBll orderMaterialBll)
        {
            _orderMaterialBll = orderMaterialBll;
        }

        /// <summary>
        /// 订单材料列表视图
        /// </summary>
        /// <returns></returns>
        public IActionResult OrderMaterialListView()
        {
            return View();
        }

        /// <summary>
        /// 添加订单材料视图
        /// </summary>
        /// <returns></returns>
        public IActionResult CreateOrderMaterialView()
        {
            return View();
        }

        /// <summary>
        /// 修改订单材料视图
        /// </summary>
        /// <returns></returns>
        public IActionResult UpdateOrderMaterialView()
        {
            return View();
        }

        /// <summary>
        /// 订单材料详情视图
        /// </summary>
        /// <returns></returns>
        public IActionResult DetailsView()
        {
            return View();
        }

        ///// <summary>
        ///// 根据订单材料id获取订单材料信息
        ///// </summary>
        ///// <param name="id"></param>
        ///// <returns></returns>
        //[HttpGet]
        //public MyJsonResult GetOrderMaterialById(string id)
        //{
        //    var OrderMaterial = _orderMaterialBll.GetOrderMaterialById(id);

        //    if (OrderMaterial == null)
        //    {
        //        return MyJsonResult.GetErrorResult("订单材料不存在");
        //    }
        //    else
        //    {
        //        return MyJsonResult.GetSuccessResult(OrderMaterial);
        //    }
        //}

        ///// <summary>
        ///// 更新订单材料
        ///// </summary>
        ///// <returns></returns>
        //[HttpPost]
        //public MyJsonResult UpdateOrderMaterial(string id, string title, string content)
        //{
        //    //参数验证
        //    if (string.IsNullOrEmpty(id))
        //    {
        //        return MyJsonResult.GetErrorResult("订单材料id不能为空");
        //    }
        //    if (string.IsNullOrEmpty(title))
        //    {
        //        return MyJsonResult.GetErrorResult("订单材料名不能为空");
        //    }

        //    string msg;
        //    //调用业务逻辑层
        //    bool isSuccess = _orderMaterialBll.UpdateOrderMaterial(id, title, content ,out msg);
        //    if (isSuccess)
        //    {
        //        return MyJsonResult.GetSuccessResult();
        //    }
        //    else
        //    {
        //        return MyJsonResult.GetErrorResult(msg);
        //    }
        //}


        /// <summary>
        /// 获取订单材料列表数据
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public object GetOrderMaterialList(string orderId, string title, int page, int limit)
        {
            if (page <= 0)
            {
                return new
                {
                    code = 0,
                    msg = "页码有误",
                    count = 0,
                    data = new List<object>()
                };
            }

            if (limit <= 0)
            {
                return new
                {
                    code = 0,
                    msg = "每页条数有误",
                    count = 0,
                    data = new List<object>()
                };
            }

            int count;

            //查订单材料数据
            var list = _orderMaterialBll.GetOrderMaterialList(orderId,title, page, limit, out count);

            return new
            {
                code = 0,
                msg = "cg",
                count,
                data = list
            };
        }



        /// <summary>
        /// 获取材料列表数据
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public object GetMaterialList(string orderId,string title, int page, int limit)
        {
            if (page <= 0)
            {
                return new
                {
                    code = 0,
                    msg = "页码有误",
                    count = 0,
                    data = new List<object>()
                };
            }

            if (limit <= 0)
            {
                return new
                {
                    code = 0,
                    msg = "每页条数有误",
                    count = 0,
                    data = new List<object>()
                };
            }

            int count;

            //查材料数据
            var list = _orderMaterialBll.GetMaterialList(orderId,title, page, limit, out count);

            return new
            {
                code = 0,
                msg = "cg",
                count,
                data = list
            };
        }

        /// <summary>
        /// 创建订单材料
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public MyJsonResult CreateOrderMaterial(List<CreateOrderMaterialModel> model)
        {
            //参数验证
            if (model == null || model.Count == 0)
            {
                return MyJsonResult.GetErrorResult("请勾选对应的材料");
            }

            if (model.Any(m=> m.Num <= 0))
            {
                return MyJsonResult.GetErrorResult("材料数量不能为零或不填");
            }

            string msg;
            bool isSuccess = _orderMaterialBll.CreateOrderMaterial(model, out msg);

            if (isSuccess)
            {
                return MyJsonResult.GetSuccessResult();
            }
            else
            {
                return MyJsonResult.GetErrorResult(msg);
            }
        }

        /// <summary>
        /// 软删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        public MyJsonResult DeleteOrderMaterial(string id)
        {
            if (string.IsNullOrEmpty(id))
            {
                return MyJsonResult.GetErrorResult("参数有误");
            }

            bool isSuccess = _orderMaterialBll.DeleteOrderMaterial(id);

            if (isSuccess)
            {
                return MyJsonResult.GetSuccessResult();
            }
            else
            {
                return MyJsonResult.GetErrorResult("失败");
            }
        }

        /// <summary>
        /// 批量软删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        public MyJsonResult DeleteOrderMaterials(List<string> ids)
        {
            if (ids.Count <= 0 || ids == null)
            {
                return MyJsonResult.GetErrorResult("参数有误");
            }

            bool isSuccess = _orderMaterialBll.DeleteOrderMaterials(ids);

            if (isSuccess)
            {
                return MyJsonResult.GetSuccessResult();
            }
            else
            {
                return MyJsonResult.GetErrorResult("失败");
            }
        }

    }

}
